import {setCurrentRouterName} from "@/views/checkPage/util/setCurrentRouterName";
import {getQuestionTableColum} from "@/views/checkBusPage/components/questionTable/tableConifg";
import QuestionTableColum from "@/views/checkBusPage/components/questionTable/index.vue";
import {queryOrgCondition, queryQuestionTypeCondition} from "@/api/checkPage/defectQuestionQuery";
import {NODE_TYPE, SCENE_STATE} from "@/constantData";

const subPageMixin = {

  components: {
    QuestionTableColum
  },
  props: {
    inspectInfo: {
      type: Object,
      default: () => {
        return {}
      }
    },
    handelCode: {
      type: [String, Number]
    },
  },
  data() {
    let owner = this
    return {
      showGoBack: false,
      questionTableColum: [],
      orgParamQuery: queryOrgCondition,
      questionTypeParamQuery: queryQuestionTypeCondition,
      orgParamDataList: [],
      questionTypeParamDataList: [],
      sceneStateList: SCENE_STATE,
      orgParamProps: {
        checkStrictly: true, lazy: true, lazyLoad(node, resolve) {
          if (node?.data?.leaf) {
            resolve()
          } else {
            queryOrgCondition(node?.data).then(res => {
              const nodes = res.data.map(item => {
                item.leaf = item.isLeaf == NODE_TYPE.LEAF;
                if (!owner.orgParamDataList.some(cItem => cItem.type == item.type && cItem.value == item.value)) {
                  owner.orgParamDataList.push(item)
                }
                return item
              })
              resolve(nodes)
            })
          }

        }
      },
      questionTypeParamProps: {
        checkStrictly: true, lazy: true, lazyLoad(node, resolve) {
          if (node?.data?.leaf) {
            resolve()
          } else {
            queryQuestionTypeCondition(node?.data).then(res => {
              const nodes = res.data.map(item => {
                item.leaf = item.isLeaf == NODE_TYPE.LEAF;
                if (!owner.questionTypeParamDataList.some(cItem => cItem.type == item.type && cItem.value == item.value)) {
                  owner.questionTypeParamDataList.push(item)
                }
                return item
              })
              resolve(nodes)
            })
          }
        }
      }
    }
  },
  methods: {
    goInfoPage(row) {
      let {inspectInfo: {id: inspectId}, handelCode} = this;
      const path = '/securityCheck/checkBusPageInfo'
      setCurrentRouterName(handelCode, path)
      this.$router.push({path, query: {handelCode, inspectId, dataId: row.id}})
    },
    reloadInfoData(id) {
      if (this.infoOpen) {
        let owner = this
        this.getList()
        let queryId = id || this.form.id
        this.getInfoData(queryId).then(data => {
          owner.infoData = data
          if (owner.$refs['danInfo']) {
            owner.$refs['danInfo'].reload()
          }
        })
      }
    }
  },
  created() {
    this.questionTableColum = getQuestionTableColum(this.handelCode)
  }
}

export {subPageMixin}
